package com.training.admin.web.controller.data;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.entity.exception.BusinessException;
import com.demo2do.core.entity.page.Page;
import com.demo2do.core.entity.result.HttpResult;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.core.web.utils.WebUtils;
import com.training.admin.dto.CourseRequest;
import com.training.admin.entity.course.Course;
import com.training.admin.entity.search.CourseSearch;
import com.training.admin.entity.trainer.Trainer;
import com.training.admin.service.CourseService;
import com.training.admin.service.DataService;

/**
 * 
 * @author Rockett
 * 
 */
@Controller
@RequestMapping("/courses")
@MenuSetting("menu-system-management")
public class CourseController {

	private static final Logger logger = LoggerFactory.getLogger(CourseController.class);

	@Autowired
	private CourseService courseServices;

	@Autowired
	private CourseService courseService;
	@Autowired
	@Qualifier("dataService")
	private DataService<Trainer> trainerService;

	/**
	 * 列表页
	 * 
	 * @return
	 */
	@RequestMapping(value = "", method = RequestMethod.GET)
	@MenuSetting("submenu-course-management")
	public ModelAndView list(Page page, HttpServletRequest request) {

		ModelAndView modelAndView = new ModelAndView("course/course-list");

		int totals = courseService.count(Course.class);

		if (totals > 0) {

			List<Course> course = courseService.listByPage(Course.class, page.with(totals));

			modelAndView.addObject("course", course);

			modelAndView.addObject("page", page);
		}

		modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "filter", "page"));

		return modelAndView;
	}

	/**
	 * 进入详情
	 * 
	 * @return
	 */
	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	@MenuSetting("submenu-course-management")
	public ModelAndView view(@PathVariable("id") Long id) {

		ModelAndView modelAndView = new ModelAndView("course/course-view");

		Course course = courseService.load(Course.class, id);
		modelAndView.addObject("course", course);

		// trainer list
		List<Trainer> trainers = trainerService.listAll(Trainer.class);
		modelAndView.addObject("trainers", trainers);

		return modelAndView;
	}

	@RequestMapping(value = "/search", method = RequestMethod.GET)
	@MenuSetting("submenu-course-management")
	public ModelAndView search() {
		ModelAndView modelAndView = new ModelAndView("course/course-search");
		return modelAndView;
	}

	
	@RequestMapping(value = "", method = RequestMethod.GET, params = "filter")
	@MenuSetting("submenu-course-management")
	public ModelAndView onSearch(CourseSearch courseSearch, Page page, HttpServletRequest request) {
		ModelAndView modelAndView = new ModelAndView("course/course-list");

		int totals = courseService.count(Course.class, courseSearch.getFilter());
		
		if(totals > 0) {
			
			List<Course> course = courseService.search(Course.class, courseSearch, page.with(totals));
			
			modelAndView.addObject("course", course);
			
			modelAndView.addObject("page", page);
			
			modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "filter", "page"));
		}
		
		return modelAndView;

	}

	/**
	 * 打开修改面板
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/{id}/edit", method = RequestMethod.GET)
	@MenuSetting("submenu-course-management")
	public ModelAndView edit(@PathVariable("id") Long id) {
		ModelAndView modelAndView = new ModelAndView("course/course-edit");
		Course course = courseService.load(Course.class, id);
		modelAndView.addObject("course", course);
		// trainer list
		List<Trainer> trainers = trainerService.listAll(Trainer.class);
		modelAndView.addObject("trainers", trainers);

		return modelAndView;
	}

	/**
	 * 修改
	 * 
	 * @param id
	 * @param course
	 * @return
	 */
	@RequestMapping(value = "/{id}", method = RequestMethod.PATCH, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@ResponseBody
	@MenuSetting("submenu-course-management")
	public HttpResult onEdit(@PathVariable("id") Long id, Course course) {
		HttpResult httpResult = new HttpResult();
		try {
			courseServices.edit(id, course);
			httpResult.success().message("修改成功");
		} catch (Exception e) {
			logger.error("onEdit occurs error", e);
			httpResult.fail("500").message("修改失败");
		}
		return httpResult;
	}

	/**
	 * 打开删除面板
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/{id}/delete", method = RequestMethod.GET)
	@MenuSetting("submenu-course-management")
	public ModelAndView disable(@PathVariable("id") Long id) {
		ModelAndView modelAndView = new ModelAndView("course/course-delete");
		return modelAndView;
	}

	/**
	 * 删除
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/{id}/delete", method = RequestMethod.PATCH, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@ResponseBody
	public HttpResult onDisabled(@PathVariable("id") Long id) {

		HttpResult httpResult = new HttpResult();

		try {

			courseServices.delete(id);
			httpResult.success().message("删除成功");

		} catch (Exception e) {

			logger.error("onDisabled occurs error", e);
			httpResult.fail("500").message("删除失败");
		}

		return httpResult;
	}

	/**
	 * 点击创建按钮
	 * 
	 * @return
	 */
	@RequestMapping(value = "/create", method = RequestMethod.GET)
	@MenuSetting("submenu-course-management")
	public ModelAndView create() {
		ModelAndView modelAndView = new ModelAndView("course/course-create");
		// trainer list
		List<Trainer> trainers = trainerService.listAll(Trainer.class);
		modelAndView.addObject("trainers", trainers);

		return modelAndView;
	}

	/**
	 * 创建提交
	 * 
	 * @return
	 */
	@RequestMapping(value = "", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@ResponseBody
	@MenuSetting("submenu-course-management")
	public HttpResult onCreate(CourseRequest courseRequest) {

		HttpResult httpResult = new HttpResult();

		try {

			courseServices.create(courseRequest);
			httpResult.success().message("创建成功");

		} catch (BusinessException e) {

			logger.error("onCreate occurs error", e);
			httpResult.fail(e.getCode()).message(e.getMessage());

		} catch (Exception e) {

			logger.error("onCreate occurs error", e);
			httpResult.fail("500").message("创建失败");

		}

		return httpResult;
	}
	
}
